package com.example.ximalayademo.adapters;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ximalayademo.R;
import com.ximalaya.ting.android.opensdk.model.word.QueryResult;

import java.util.ArrayList;
import java.util.List;

public class SearchRecommendAdapter extends RecyclerView.Adapter<SearchRecommendAdapter.InnerHolder> {
    List<QueryResult> myKeyWordList = new ArrayList<>();
    private ItemClickListener mItemClick = null;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_tecommend, parent, false);
        return new InnerHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        QueryResult queryResult = myKeyWordList.get(position);
        String keyword = queryResult.getKeyword();
        holder.mRecommendTitle.setText(keyword);
        holder.mSearchBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClick != null) {
                    mItemClick.onItemClick(keyword);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return myKeyWordList.size();
    }

    public void setData(List<QueryResult> keyWordList) {
        myKeyWordList.clear();
        myKeyWordList.addAll(keyWordList);
        notifyDataSetChanged();
    }


    public class InnerHolder extends RecyclerView.ViewHolder {

        private final TextView mRecommendTitle;
        private final View mSearchBox;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            mSearchBox = itemView.findViewById(R.id.search_box);
            mRecommendTitle = itemView.findViewById(R.id.search_recommend_item);
        }
    }

    public void setItemClickListener(ItemClickListener listener){
        this.mItemClick = listener;
    }

    public interface ItemClickListener{
        void onItemClick(String keyword);
    }
}
